package org.zk.controller;

import com.google.gson.Gson;
import org.zk.entity.OrdersExt;
import org.zk.entity.User;
import org.zk.service.OrdersService;
import org.zk.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    private Gson gson = new Gson();

    /**
     * 查询所有订单
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderses", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String getOrderses(HttpServletRequest request) {

        Msg msg = new Msg(0);
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        if (currentUser == null) {
            msg.setMessage("用户未登录");
            return gson.toJson(msg);
        }else{

            List<OrdersExt> orderses = ordersService.getOrderses(currentUser.getId());
            if(orderses!= null){

                msg.setCode(1);
                msg.setData(orderses);
                return gson.toJson(msg);
            }
        }
        return gson.toJson(msg);
    }


}
